/*
 * Copyright (c) 2012 Johan Fylling
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights 
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 * THE SOFTWARE.
 */

part of TopDownExample;


class PlayerController extends LogicController {
  Camera camera;
  num    speed   = 5.0;
  
  PlayerController (Camera this.camera) {
    InputManager inputManager = ServiceManager.getService("InputManager") as InputManager;
    inputManager.addAction("MOVE_UP", [new KeyboardAction(Keyboard.KEY_UP), new KeyboardAction(Keyboard.KEY_W)]);
    inputManager.addAction("MOVE_DOWN", [new KeyboardAction(Keyboard.KEY_DOWN), new KeyboardAction(Keyboard.KEY_S)]);
    inputManager.addAction("MOVE_LEFT", [new KeyboardAction(Keyboard.KEY_LEFT), new KeyboardAction(Keyboard.KEY_A)]);
    inputManager.addAction("MOVE_RIGHT", [new KeyboardAction(Keyboard.KEY_RIGHT), new KeyboardAction(Keyboard.KEY_D)]);
  }
  
  void update (Percept percept) {
    num health = percept.getNumAttribute("health");
    
    InputManager inputManager = ServiceManager.getService("InputManager") as InputManager;
    
    Vector2 direction = new Vector2();
    Keyboard keyboard = (inputManager.getMethod(Keyboard.INPUT_TYPE) as Keyboard);
    
    if (inputManager.isActionSet("MOVE_UP")) {
      direction.y -= 1;
    }
    if (inputManager.isActionSet("MOVE_DOWN")) {
      direction.y += 1;
    }
    if (inputManager.isActionSet("MOVE_LEFT")) {
      direction.x -= 1;
    }
    if (inputManager.isActionSet("MOVE_RIGHT")) {
      direction.x += 1;
    }
    
    Mouse mouse = (inputManager.getMethod(Mouse.INPUT_TYPE) as Mouse);
    Vector2 orientation = this.camera.canvas2world(mouse.position) - percept.position;
    if (mouse.isWheelUp()) {
      this.camera.scale = this.camera.scale*1.1;
    } else if (mouse.isWheelDown()) {
      this.camera.scale = this.camera.scale*0.9;
    }
    
    if (mouse.isStateSet(Mouse.BUTTON_LEFT)) {
      Inventory inventory = percept.inventory;
      if (!inventory.content.isEmpty) {
        UseAction useAction = new UseAction(inventory.getEquiped("right_hand"));
        percept.addAction(useAction);
      }
    }
    
    MovementAction action = new MovementAction();
    action.direction = direction;
    action.speed = speed;
    action.orientation = orientation;
    percept.movementAction = action;
    
    num deltaSpeed = action.speed / 1e3;
    this.camera.position.clone(percept.position + action.direction*deltaSpeed);
  }
  
  void use (Percept percept, Percept userPercept) {
    
  }
}
